package demo.c1;

import lombok.SneakyThrows;

/**
 * TicketWindowRunnable
 *
 * @author chisheng
 */
public class TicketWindowRunnable implements Runnable {

    /**
     * Ticket index number. Not use static, but it is implement
     * by Runnable, the thread can share the contruct instance.
     *
     * so the index never happened repetication.
     */
    private int index = 1;

    /**
     * Ticket max number
     */
    private final static int MAX = 50;

    @Override
    @SneakyThrows
    @SuppressWarnings("all")
    public void run() {
        while (index <= MAX) {
            System.out.println(Thread.currentThread().getName() + "'s number: " + index++);
            Thread.sleep(100);
        }
    }

    /**
     * Test
     */
    public static void main(String[] args) {
        final TicketWindowRunnable ticketWindow = new TicketWindowRunnable();

        // The TicketWindowRunnable instance will be shared among the Threads
        final Thread thread0 = new Thread(ticketWindow, "一号窗口");
        final Thread thread1 = new Thread(ticketWindow, "二号窗口");
        final Thread thread2 = new Thread(ticketWindow, "三号窗口");

        thread0.start();
        thread1.start();
        thread2.start();
    }

}
